package com.example.module_web.view.fragment;

import android.content.Context;
import android.content.Entity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.example.module_web.R;
import com.example.module_web.adapter.WaterBoxAdapter;
import com.example.module_web.beans.WaterBox;
import com.example.module_web.public_data.Constant;
import com.example.module_web.view.dialog.AddProduct_Dialog;
import com.example.module_web.view.dialog.IAddProductData;
import com.example.module_web.viewmodule.NetData_Web;
import com.github.mikephil.charting.charts.CombinedChart;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;

import org.json.JSONException;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import Util.MPChartUtils;

import static android.content.Context.INPUT_METHOD_SERVICE;

/**
 * Created by ASUS on 2018/3/14.
 */

public class Fragment_21 extends FormFragment {
    CombinedChart mLineChart0,mLineChart1,mLineChart2;
    FloatingActionButton mFloatingActionButton;
    private RecyclerView mRecyWaterBox;

    @Override
    protected int setLayoutId() {
        return R.layout.web_fragment_21;
    }

    @Override
    protected void initView(View view) {
        mRecyWaterBox = view.findViewById(R.id.web_frag21_recy_waterbox);
        mLineChart0 = view.findViewById(R.id.linechart0);
        mLineChart1 = view.findViewById(R.id.linechart1);
        mLineChart2 = view.findViewById(R.id.linechart2);
        mFloatingActionButton=view.findViewById(R.id.float_button);
        mFloatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AddProduct_Dialog(getContext(),mIAddProductData).show();
            }
        });

    }
    public IAddProductData mIAddProductData=new IAddProductData() {
        @Override
        public void Data(String[] strings) {
            NetData_Web.getInstance().addProductData(getContext(), "添加每日数据", strings, Constant.CHILDREN[2][1], new NetData_Web.Singer<Boolean>() {
                @Override
                public void success(Boolean s) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, JSONException {
                    if (s){
                        Toast.makeText(getContext(), "添加成功!", Toast.LENGTH_SHORT).show();
                        if(getActivity().getCurrentFocus()!=null)
                        {
                            ((InputMethodManager) getContext().getSystemService(INPUT_METHOD_SERVICE))
                                    .hideSoftInputFromWindow((getActivity().getCurrentFocus().getWindowToken()),InputMethodManager.HIDE_NOT_ALWAYS);
                        }
                    }else {
                        Toast.makeText(getContext(), "添加失败!", Toast.LENGTH_SHORT).show();
                    }
                }
                @Override
                public void failed(String s) {

                }
            });
        }
    };
    @Override
    protected void initEvent(Context context, Bundle savedInstanceState) {
        setupRecyclerData(context);
    }

    public void setupRecyclerData(Context context) {
        setupRecycler(context,mRecyWaterBox);
        loadNetData(context);
    }

    private void loadNetData(Context context) {
        NetData_Web.getInstance().getWaterBoxes(context, "GetProductData", "补水套盒",new NetData_Web.Singer<List<WaterBox>>() {
            @Override
            public void success(List<WaterBox> waterBoxes2) {
                Log.e(TAG, "success: waterBoxer = "+waterBoxes2);
                WaterBoxAdapter waterBoxAdapter = new WaterBoxAdapter(R.layout.web_item_frag2_public_product,waterBoxes2);
                mRecyWaterBox.setAdapter(waterBoxAdapter);
                List<WaterBox> waterBoxes = new ArrayList<>(waterBoxes2);
                Collections.sort(waterBoxes,new Comparator<WaterBox>(){
                    @Override
                    public int compare(WaterBox o1, WaterBox o2) {
                        return o1.getDate().compareTo(o2.getDate())>0?1:-1;
                    }
                });
                int max=0,max2 = 0,max3=0;
                ArrayList<String> names=new ArrayList<>();
                ArrayList<Entry> values0=new ArrayList<>();
                ArrayList<Entry> values1=new ArrayList<>();
                ArrayList<Entry> values2=new ArrayList<>();
                ArrayList<Entry> values3=new ArrayList<>();
                ArrayList<Entry> values4=new ArrayList<>();
                Entry _entry,_entry1,_entry2,_entry3,_entry4;
                for (int i = 0; i < waterBoxes.size(); i++) {
                    names.add(waterBoxes.get(i).getDate().substring(5,10));
                    if (waterBoxes.get(i).getLiulan()>max){
                        max=waterBoxes.get(i).getLiulan();
                    }
                    if (waterBoxes.get(i).getJiagou()>max2){
                        max2=waterBoxes.get(i).getJiagou();
                    }
                    if (waterBoxes.get(i).getTingliu()>max3){
                        max3=(int)waterBoxes.get(i).getTingliu();
                    }
                    _entry=new Entry(i,waterBoxes.get(i).getFangke());
                    _entry1=new Entry(i,waterBoxes.get(i).getLiulan());
                    _entry2=new Entry(i,waterBoxes.get(i).getJiagou());
                    _entry3=new Entry(i,waterBoxes.get(i).getShoucang());
                    _entry4=new Entry(i,(float) waterBoxes.get(i).getTingliu());
                    values0.add(_entry);
                    values1.add(_entry1);
                    values2.add(_entry2);
                    values3.add(_entry3);
                    values4.add(_entry4);
                }
                max=((int)((float)(max+50.0f)/50.0f)*50);
                max2=(int)((float)(max2+5.0f)/5.0f*5.0f);

                max3=(int)((float)(max3+10.0f)/10.0f*10.0f);
//                Collections.reverse(values0);
//                Collections.reverse(values1);
//                Collections.reverse(values2);
//                Collections.reverse(values3);
//                Collections.reverse(values4);
                MPChartUtils.configChart(mLineChart0, names, max, 0, true,Color.BLACK);
                LineDataSet tartgetDataSet = MPChartUtils.getLineData(values0, "访客", Color.BLACK, Color.BLACK, true);
                LineDataSet lineDataSet = MPChartUtils.getLineData(values1, "浏览量", Color.BLACK, Color.BLUE, true);
                MPChartUtils.initData(mLineChart0, new LineData(lineDataSet, tartgetDataSet));

                MPChartUtils.configChart(mLineChart1, names, max2, 0, true,Color.BLACK);
                LineDataSet tartgetDataSet1 = MPChartUtils.getLineData(values3, "收藏人数", Color.BLACK, Color.BLACK, true);
                LineDataSet lineDataSet1 = MPChartUtils.getLineData(values2, "加购物车", Color.BLACK, Color.BLUE, true);
                MPChartUtils.initData(mLineChart1, new LineData(lineDataSet1, tartgetDataSet1));

                MPChartUtils.configChart(mLineChart2, names, max3, 0, true,Color.BLACK);
                LineDataSet tartgetDataSet2 = MPChartUtils.getLineData(values4, "停留时长", Color.BLACK, Color.BLACK, true);
                MPChartUtils.initData(mLineChart2, new LineData(tartgetDataSet2));

            }
            @Override
            public void failed(String s) {

            }
        });
    }
}
